package com.hsck.order.kds.main.dialog

import android.app.Activity
import android.app.Dialog
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.AdapterView
import androidx.databinding.DataBindingUtil
import com.hjq.toast.Toaster
import com.hsck.order.kds.R
import com.hsck.order.kds.base.utils.setOnSingleClickListener
import com.hsck.order.kds.databinding.SelectNumberDialogLayoutBinding

/**
 * 标准划菜选择划菜数量弹窗
 */
class SelectNumberDialog(context: Activity) : Dialog(context, R.style.Dialog_FullScreen) {
    private lateinit var selectNumberListener: SelectNumberListener
    private val activity: Activity = context
    private var valueList: ArrayList<Map<String, String>>? = null
    private var allNumber = ""
    private var change = false

    private var binding: SelectNumberDialogLayoutBinding = DataBindingUtil.inflate(
        LayoutInflater.from(context),
        R.layout.select_number_dialog_layout,
        null,
        false
    )

    init {
        createDialog()
    }

    private fun createDialog() {
        setContentView(binding.root)
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        val lp = window?.attributes
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        lp?.height = WindowManager.LayoutParams.MATCH_PARENT

        lp?.alpha = 1f
        window?.attributes = lp
        if (activity.isFinishing) {
            return
        }
        binding.viewClose.setOnSingleClickListener { dismiss() }
        binding.tvOk.setOnSingleClickListener {
            val number = binding.tvNumber.text.toString()
            if (!change && number.isEmpty()) {
                Toaster.show("请输入数量")
                return@setOnSingleClickListener
            }
            selectNumberListener.onSelectNumber(if (number.isNotEmpty()) number.toInt() else 0)
            dismiss()
        }
        binding.virtualKeyboardView.initValueList()
        valueList = binding.virtualKeyboardView.getValueList()
    }

    private val onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ ->
        if (position < 11 && position != 9) {    //点击0~9按钮
            var inputNumber = binding.tvNumber.getText().toString().trim { it <= ' ' }
            var selectNumber: String = binding.tvNumber.getText().toString().trim { it <= ' ' }
            inputNumber += valueList!![position]["name"]
            if (inputNumber.isNotEmpty() && allNumber.isNotEmpty()) {
                if (inputNumber.toInt() > allNumber.toInt()) {
                    Toaster.show("输入的划菜数量不能大于总数量")
                    return@OnItemClickListener
                }
            }
            selectNumber += valueList!![position]["name"]
            binding.tvNumber.text = selectNumber
        } else {
            if (position == 9) {
                var amount: String = binding.tvNumber.getText().toString().trim { it <= ' ' }
                if (amount.isNotEmpty()) {
                    amount = ""
                    binding.tvNumber.text = amount
                }
            }
            if (position == 11) {      //点击退格键
                var amount: String = binding.tvNumber.getText().toString().trim { it <= ' ' }
                if (amount.isNotEmpty()) {
                    amount = amount.substring(0, amount.length - 1)
                    binding.tvNumber.text = amount
                }
            }
        }
    }

    fun show(dishName: String, allNumber: String, change: Boolean = false) {
        show()
        this.change = change
        if (change) {
            binding.tvOk.text = "确认数量"
        }
        this.allNumber = allNumber
        binding.tvDishName.text = dishName
        binding.tvAllNumber.text = allNumber
        binding.tvNumber.text = allNumber
        binding.virtualKeyboardView.getGridView().onItemClickListener = onItemClickListener
    }

    fun setSelectNumberListener(selectNumberListener: SelectNumberListener) {
        this.selectNumberListener = selectNumberListener
    }

    interface SelectNumberListener {
        fun onSelectNumber(i: Int)
    }
}
